﻿using Cms.Core.Infrastructure.Common;
using Cms.Core.Infrastructure.Extension;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;

namespace Cms.Core.Infrastructure.Ui
{
   public static class AjaxResultExtensions
    {
        public static AjaxResult ToAjaxResult(this OperationResult operationResult)
        {
            var message = operationResult.Message ?? operationResult.Type.ToDisplayName();
            AjaxResultType type = operationResult.Type.ToAjaxResultType();
            return new AjaxResult(message, type, operationResult.Data) { Success = operationResult.Successed };
        }

        public static AjaxResult ToAjaxResult<T>(this OperationResult<T> operationResult)
        {
            var message = operationResult.Message ?? operationResult.Type.ToDisplayName();
            AjaxResultType type = operationResult.Type.ToAjaxResultType();
            return new AjaxResult(message, type, operationResult.Data) { Success = operationResult.Successed };
        }

        public static AjaxResultType ToAjaxResultType(this OperationResultType resultType)
        {
            switch (resultType)
            {
                case OperationResultType.Success:
                    return AjaxResultType.Success;
                case OperationResultType.NoChanged:
                    return AjaxResultType.Info;
                default:
                    return AjaxResultType.Error;
            }
        }
    }
}
